<?php
/*
 *@Note  授权用户组管理控制器
 * @Date 2024-01-05
 * @aurh Milo·Wang
 * @email 1173261319@qq.com
 */

namespace app\system\controller;



class AuthGroup extends Base
{
    /**
     * @Notes 用户组列表首页
     */
    public function index()
    {
        if (request()->isPost()) {
            $title = input('title','');
            $where = [];
            if($title) $where['title'] = ['like','%'.$title.'%'];
            $list = model('AuthGroup')->where($where)->select();
            foreach ($list as $k => $v) {
                $list[$k]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            }
            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list, 'count'=> 1,'rel' => 1];
        }
        return $this->fetch();
    }
    /**
     * @Notes 添加用户组
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['addtime'] = time();
            $result = model('AuthGroup')->insert($data);
            if ($result !== false) {
                return ajaxArray(1, '用户组添加成功');
            }
            return ajaxArray(0, '用户组添加失败');
        }
        return $this->fetch();
    }

    /**
     * @Notes 编辑用户组
     */
    public function edit()
    {
        $id = input('id/d', 0);
        if (request()->isPost()) {
            $data = input('post.');
            $res = model('AuthGroup')->where(['group_id' => $data['id']])->update(['title' => $data['title']]);
            if ($res) {
                return ajaxArray(1, '编辑成功');
            }
            return ajaxArray(0, '编辑失败');
        }
        $authgroup = model('AuthGroup')->where(['group_id' => $id])->find();
        $this->assign('authgroup', $authgroup);
        return $this->fetch();
    }

    /**
     * @Notes 删除
     */
    public function del() {
        $id = input('id/d',0);
        if(empty($id)) return ajaxArray(0,'数据错误');
        $canDo = model('admin')->where('group_id',$id)->count();
        if($canDo > 0) return ajaxArray(0,'当前用户组有业务关联,无法删除!');
        // 硬删除
        $res = model('AuthGroup')->destroy($id);
        if($res) return ajaxArray(1,'删除成功');
        return ajaxArray(0,'删除失败');
    }

    /**
     * @Notes 授权
     */
    public function access()
    {
        $id = input('id/d', 0);
        $this->assign('id', $id);
        $rules = model('AuthGroup')->where('group_id', $id)->value('rules');
        $ids = explode(',', $rules);
        $this->assign('data', json_encode($this->auth(0, $ids)));
        $this->assign('checked', json_encode($ids, JSON_NUMERIC_CHECK));
        return $this->fetch();
    }
    private function auth($pid, &$ids)
    {
        $menus = db('system_menu')->field('id,title,pid')->where('pid', $pid)->order('sort')->select();
        if ($menus) {
            $ids = array_merge(array_diff($ids, array($pid)));
            foreach ($menus as $k => $v) {
                $menus[$k]['spread'] = true;
                $menus[$k]['children'] = $this->auth($v['id'], $ids);
            }
        }
        return $menus;
    }
    /**
     * @Notes 设置授权
     */
    public function setaccess()
    {
        $group_id = input('group_id/d', 0);
        $rules = input('rules/a');
        if (empty($rules)) {
            return ajaxArray(0,'请选择权限');
        }
        $ids = [];
        $this->getIds($rules, $ids);
        $result = model('AuthGroup')->where('group_id', $group_id)->setField('rules', join(',', $ids));
        if ($result !== false) {
            return ajaxArray(1, '权限配置成功');
        }
        return ajaxArray(0, '保存错误');
    }
    private function getIds($rules, &$ids)
    {
        if ($rules) {
            foreach ($rules as $k => $v) {
                $ids[] = $v['id'];
                if (isset($v['children'])) $this->getIds($v['children'], $ids);
            }
        }
    }
}
